import 'package:flutter/cupertino.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutterapp/Base/style/MyStyles.dart';
import 'package:fluttertoast/fluttertoast.dart';

Header refreshHeader = new ClassicalHeader();
Footer refreshFooter = new ClassicalFooter(triggerDistance: 250);

class PageNoData extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color(0xffe9e9e9),
      child: Column(
        children: [
          SizedBox(
            height: 100,
          ),
          // Image.asset("images/pic_no_order.png",width: 140,),
          Text(
            "暂无数据",
            style: MyTextStyle.GREY3_18,
          ),
          Text("请稍后再试", style: MyTextStyle.GREY6_14)
        ],
      ),
    );
  }
}

typedef OnRefresh = Function(int page, RefreshController callBack);
typedef OnLoad = Function(int page, LoadController callBack);
typedef RefreshCallBack = Function(bool success, bool showEmptyView);
typedef LoadCallBack = Function(bool success, bool hasMore);

class RefreshController {
  RefreshCallBack _refreshCallBack;

  RefreshController(this._refreshCallBack);

  // 只有刷新结束时 获取是否显示空视图才有意义 加载结束时获取没意义
  finishRefresh({success = true, showEmptyView = false}) {
    this._refreshCallBack(success, showEmptyView);
  }
}

class LoadController {
  LoadCallBack _loadCallBack;

  LoadController(this._loadCallBack);

  finishLoad({success = true, hasMore = true}) {
    this._loadCallBack(success, hasMore);
  }
}

class RefreshLoadWrapper extends StatefulWidget {
  final int initPage;
  final Widget child;
  final OnRefresh onRefresh;
  final OnLoad onLoad;

  RefreshLoadWrapper(this.child, this.onRefresh, this.onLoad,
      {this.initPage = 1, Key key})
      : super(key: key);

  @override
  _RefreshLoadWrapperState createState() => _RefreshLoadWrapperState();
}

class _RefreshLoadWrapperState extends State<RefreshLoadWrapper> {
  EasyRefreshController _easyRefreshController = EasyRefreshController();
  int page;
  bool isEmpty = false;
  OnRefresh _onRefresh;
  OnLoad _onLoad;
  LoadController loadController;
  RefreshController refreshController;

  @override
  void initState() {
    super.initState();
    page = widget.initPage;
    _onRefresh = widget.onRefresh;
    _onLoad = widget.onLoad;
    loadController = new LoadController(loadCallBack);
    refreshController = new RefreshController(refreshCallBack);
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh(
      controller: _easyRefreshController,
      enableControlFinishRefresh: true,
      enableControlFinishLoad: true,
      onRefresh: () async {
        page = 1;
        _onRefresh(page, refreshController);
      },
      onLoad: () async {
        // await widget.onLoad;
        int nextPage = page + 1;
        _onLoad(nextPage, loadController);
      },
      child: widget.child,
      emptyWidget: isEmpty ? PageNoData() : null,
      header: refreshHeader,
      footer: refreshFooter,
    );
  }

  refreshCallBack(bool success, bool showEmptyView) {
    _easyRefreshController.finishRefresh(success: success);
    if (!success) {
      Fluttertoast.showToast(msg: "加载失败");
    }
    isEmpty = showEmptyView;
    setState(() {});
  }

  loadCallBack(bool success, bool hasMore) {
    // 是否加载完毕不由底层控制
    _easyRefreshController.finishLoad(success: success);
    if (success) {
      if (!hasMore) {
        Fluttertoast.showToast(msg: "暂无更多数据");
      } else {
        page++;
      }
    } else {
      Fluttertoast.showToast(msg: "加载失败");
    }
    setState(() {});
  }

  //可使用globalKey主动控制
  showEmptyView() {
    isEmpty = true;
    setState(() {});
  }
}
